import numpy as np
from PIL import Image
from osgeo import gdal
import statistics

def get_direction(image_file):
    # 加载预测图像
    prediction_image = Image.open(image_file)

    # 转换为灰度图像
    prediction_image_gray = prediction_image.convert("L")

    # 获取图像尺寸
    width, height = prediction_image_gray.size
    pixels = [[0 for _ in range(height)] for _ in range(width)]
    # 获取像素值
    for i in range(height):
        for j in range(width):
            pixels[j][i] = prediction_image_gray.getpixel((j,i))

    # 计算每个边缘的白色像素占比
    top_edge_white = sum(1 for x in [pixels[j][0] for j in range(width)] if x == 255) / width
    bottom_edge_white = sum(1 for x in [pixels[j][height - 1] for j in range(width)] if x == 255) / width
    left_edge_white = sum(1 for x in [pixels[0][i] for i in range(height)] if x == 255) / height
    right_edge_white = sum(1 for x in [pixels[width - 1][i] for i in range(height)] if x == 255) / height

    # 计算上下边缘的白色像素占比之和和左右边缘的白色像素占比之和
    top_bottom_ratio = top_edge_white + bottom_edge_white
    left_right_ratio = left_edge_white + right_edge_white

    # 比较两个之和
    if top_bottom_ratio > left_right_ratio:
        direction = "ver"
    else:
        direction = "hor"
    print(image_file, " direction : ", direction)

# get_direction('cropped/cropped_image_28942973.tif')